package com.base.self.service.impl;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.base.result.PageUtils;
import com.base.result.ResponseResult;
import com.base.result.ResponseResultUtils;
import com.base.self.common.SelfRuntimeException;
import com.base.self.dto.commodityReferrer.CommodityReferrerDeleteDTO;
import com.base.self.dto.commodityReferrer.CommodityReferrerWebAddDTO;
import com.base.self.dto.commodityReferrer.CommodityReferrerWebListDTO;
import com.base.self.entity.Commodity;
import com.base.self.entity.CommodityReferrer;
import com.base.self.mapper.CommodityMapper;
import com.base.self.mapper.CommodityReferrerMapper;
import com.base.self.service.ICommodityReferrerService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Map;
import java.util.Optional;

/**
 * <p>
 * 商品推荐 服务实现类
 * </p>
 *
 * @author XiFYuW
 * @since 2020-11-15
 */
@Service
public class CommodityReferrerServiceImpl extends ServiceImpl<CommodityReferrerMapper, CommodityReferrer> implements ICommodityReferrerService {

    private final CommodityMapper commodityMapper;

    private final CommodityReferrerMapper commodityReferrerMapper;

    public CommodityReferrerServiceImpl(CommodityMapper commodityMapper, CommodityReferrerMapper commodityReferrerMapper) {
        this.commodityMapper = commodityMapper;
        this.commodityReferrerMapper = commodityReferrerMapper;
    }

    @Override
    public ResponseResult commodityReferrerWebList(CommodityReferrerWebListDTO commodityReferrerWebListDTO) {
        String classifyName = commodityReferrerWebListDTO.getClassifyName();
        final Map<String, Object> data = PageUtils.getDateMap(() -> page(PageUtils.getPage(
                new Page<>(), commodityReferrerWebListDTO.getOffset(), commodityReferrerWebListDTO.getLimit()),
                new QueryWrapper<CommodityReferrer>()
                        .likeRight(!StringUtils.isEmpty(classifyName), "classify_name", classifyName)
                        .eq("is_del", 0)
                        .orderByDesc("create_time")));
        return ResponseResultUtils.getResponseResultS("查询成功", data);
    }

    @Override
    public ResponseResult commodityReferrerWebAdd(CommodityReferrerWebAddDTO commodityReferrerWebAddDTO) {
        final Commodity commodity = commodityMapper.selectById(commodityReferrerWebAddDTO.getId());
        Optional.ofNullable(commodity).orElseThrow(() -> new SelfRuntimeException(ResponseResultUtils.getResponseResultF("商品信息错误")));
        Integer count = commodityReferrerMapper.selectCount(new QueryWrapper<CommodityReferrer>()
                .eq("is_del", 0)
                .eq("commodity_id", commodityReferrerWebAddDTO.getId())
        );
        if (count > 0) {
            return ResponseResultUtils.getResponseResultS("该商品被推荐，不能重复推荐");
        }
        final CommodityReferrer commodityReferrer = CommodityReferrer
                .builder()
                .commodityId(commodity.getId())
                .commodityName(commodity.getCommodityName())
                .createTime(DateUtil.date())
                .build();
        save(commodityReferrer);
        return ResponseResultUtils.getResponseResultS("推荐成功");
    }

    @Override
    public ResponseResult commodityReferrerWebDelete(CommodityReferrerDeleteDTO commodityReferrerDeleteDTO) {
        final CommodityReferrer commodityReferrer = CommodityReferrer
                .builder()
                .id(commodityReferrerDeleteDTO.getId())
                .isDel(1)
                .updateTime(DateUtil.date())
                .build();
        updateById(commodityReferrer);
        return ResponseResultUtils.getResponseResultS("删除成功");
    }
}
